<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Founder_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	
	public function get_recent_tasks()
	{
		return $this->db->order_by('id','DESC')->get('tasks',5)->result();
	}
	
	public function active_user()
	{
		return $this->db->get_where('users',array('online_status'=>1))->result();		
	}
	
	public function inactive_user()
	{
		return $this->db->get_where('users',array('online_status'=>0))->result();		
	}
public function client_data()
{
$this->db->select("customer.id");
$this->db->from('customer');	
$q =count( $this->db->get()->result());
return $q;
}
public function product_data()
{
$this->db->select("product.id");
$this->db->from('product');	
$q =count( $this->db->get()->result());
return $q;
}
public function purchase_data()
{
$sql = "SELECT purchase.final_price\n"
    . "FROM `purchase` \n"
    . "WHERE YEAR( RECEIVED_DATE ) = YEAR( NOW( ) ) \n"
    . "AND MONTH( RECEIVED_DATE ) = MONTH( NOW( ) ) \n"
	
    . "LIMIT 0 , 30\n"
    . "";
  $q=$this->db->query($sql)->result();
  $purchase=0;
foreach ($q as $qs)
{
$purchase=$purchase+$qs->final_price;

}
return $purchase;
}
public function sale_data()
{
$sql = "SELECT sales.final_price\n"
    . "FROM `sales` \n"
    . "WHERE YEAR( DATE ) = YEAR( NOW( ) ) \n"
    . "AND MONTH( DATE ) = MONTH( NOW( ) ) \n"
	 
    . "LIMIT 0 , 30\n"
    . "";
  $q=$this->db->query($sql)->result();
$amt=0;
foreach ($q as $qs)
{
$amt=$amt+$qs->final_price;

}
return $amt;
}	
public function get_income()
{
$sql = "SELECT product.purchase_price, product.selling_price,sales_product.quantity\n"
    . "FROM `product` \n"
    . "JOIN sales_product ON product.id = sales_product.id_product\n"
    . "WHERE YEAR( sales_product.date ) = YEAR( NOW( ) ) \n"
    . " AND MONTH( sales_product.date ) = MONTH( NOW( ) ) \n";
 
  $q=$this->db->query($sql)->result();

  $amt=0;
foreach ($q as $qs)
{
$data=intval($qs->selling_price)-intval($qs->purchase_price);
$amt=$amt+intval($data * $qs->quantity);

}
if(!empty ($data))
{
return $data;
   } else return 0;
}
	
public function get_userdata($id)
{
$data= $this->db->get_where('users',array('id'=>$id))->row();		
return $data->username; 

}
public function today_purchase()
{
$sql = "SELECT purchase_product.quantity,purchase_product.total\n"
    . "FROM `purchase_product` \n"
    . "WHERE YEAR( purchase_product.date ) = YEAR( NOW( ) ) \n"
    . " AND MONTH( purchase_product.date ) = MONTH( NOW( ) ) \n"
	. "AND DATE( purchase_product.date ) = DATE( NOW( ) ) \n";
 
  $q=$this->db->query($sql)->result();
return $q;
}
public function today_sale()
{
$sql = "SELECT sales_product.quantity,sales_product.total\n"
    . "FROM `sales_product` \n"
    . "WHERE YEAR( sales_product.date ) = YEAR( NOW( ) ) \n"
    . " AND MONTH( sales_product.date ) = MONTH( NOW( ) ) \n"
	. "AND DATE( sales_product.date ) = DATE( NOW( ) ) \n";
 
  $q=$this->db->query($sql)->result();
return $q;
}


	
	}
